class Mediator {
    constructor() {
        this.components = new Set()
    }
    register (component) {
        component.mediator = this
        this.components.add(component)
    }
    notify (sender, event) {
        this.components.forEach(component => {
            if (component !== sender) {
                component.receive(sender, event)
            }
        })
    }
}

class Component {
    constructor(name) {
        this.name = name
        this.mediator = null
    }
    send (event) {
        console.log(`${this.name}发送事件${event}`)
        this.mediator.notify(this, event)
    }
    receive (sender, event) {
        console.log(`${this.name}接收${sender.name}发送的事件${event}`)
    }
}

const mediator = new Mediator()
const componentA = new Component('Component A')
const componentB = new Component('Component B')
const componentC = new Component('Component C')

mediator.register(componentA)
mediator.register(componentB)
mediator.register(componentC)

componentA.send('hello')